/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpDetalleTemporal.java
 *
 * Created on 15-nov-2010, 12:56:41
 */

package GestionMovimientos;

import administracion.BeanBinding.TblBeanBindingDetalleIngreso;
import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Utiles_MensajesDialogo;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import suministros.ejb30.TO.DetalleMovimientoTemporalTO;
import suministros.ejb30.entity.DetalleMovimientoTemporal;

/**
 *
 * @author Diana
 */
public class jpDetalleTemporal extends javax.swing.JPanel {

    jifIngresosAlmacen jpPadre;
    JDialog V_padre;
    String empresa;
    String ubicacion;
    String ruc;
    List<DetalleMovimientoTemporalTO> detalles;
    private BindingGroup bindingGroup;
   private JTableBinding jTableBinding;
    /** Creates new form jpDetalleTemporal */
    public jpDetalleTemporal(JDialog jdPadre, jifIngresosAlmacen padre, String empresa, String ubicacion, String ruc) {
        this.jpPadre = padre;
        this.V_padre = jdPadre;
        this.empresa=empresa;
        this.ubicacion=ubicacion;
        this.ruc=ruc;
        initComponents();
        this.llenarDatos();
    }
    public void llenarDatos(){
        try {
            detalles = GestionPedidosDelegate.getInstance().getListaMovimientoTemporal(ubicacion, empresa, ruc);
            this.cargar();
        } catch (Exception ex) {
            Logger.getLogger(jpDetalleTemporal.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(!detalles.isEmpty()){
            this.jbtAceptar.setEnabled(true);
        }

    }
    public void cargar(){
        this.bindingGroup = new BindingGroup();
        this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, this.detalles, this.jTable1);

        JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${codigo}"));

        columnBinding0.setColumnName("Codigo");
        columnBinding0.setColumnClass(String.class);
        columnBinding0.setEditable(false);

        JTableBinding.ColumnBinding columnBinding1 = this.jTableBinding.addColumnBinding(ELProperty.create("${suministro}"));

        columnBinding1.setColumnName("Suministro");
        columnBinding1.setColumnClass(String.class);
        columnBinding1.setEditable(false);

        JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cantidad}"));

        columnBinding2.setColumnName("cantidad");
        columnBinding2.setColumnClass(BigDecimal.class);
        columnBinding2.setEditable(false);

        JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${precio}"));

        columnBinding3.setColumnName("Precio");
        columnBinding3.setColumnClass(BigDecimal.class);
        columnBinding3.setEditable(false);

        JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

        columnBinding4.setColumnName("Fecha");
        columnBinding4.setColumnClass(String.class);
        columnBinding4.setEditable(false);

        JTableBinding.ColumnBinding columnBinding5= this.jTableBinding.addColumnBinding(ELProperty.create("${proveedor}"));;

        columnBinding5.setColumnName("Proveedor");
        columnBinding5.setColumnClass(String.class);
        columnBinding5.setEditable(false);

         JTableBinding.ColumnBinding columnBinding6= this.jTableBinding.addColumnBinding(ELProperty.create("${serie}"));;

        columnBinding6.setColumnName("Serie");
        columnBinding6.setColumnClass(String.class);
        columnBinding6.setEditable(false);

        JTableBinding.ColumnBinding columnBinding7= this.jTableBinding.addColumnBinding(ELProperty.create("${numero}"));;

        columnBinding7.setColumnName("Numero");
        columnBinding7.setColumnClass(String.class);
        columnBinding7.setEditable(false);


        this.bindingGroup.addBinding(this.jTableBinding);
        this.bindingGroup.bind();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jbtAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        jLabel1.setText("MOVIMIENTOS NO INGRESADOS");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Suministro", "Cantidad"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jbtAceptar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbtAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbtAceptar.setMnemonic('A');
        jbtAceptar.setText("ACEPTAR");
        jbtAceptar.setEnabled(false);
        jbtAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAceptarActionPerformed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CERRAR");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(jbtAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(jbCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAceptarActionPerformed
        TblBeanBindingDetalleIngreso detalle = new TblBeanBindingDetalleIngreso();
        try {
            int selec= this.jTable1.getSelectedRow();
            if(selec>=0){
                    DetalleMovimientoTemporalTO detalletempora=detalles.get(this.jTable1.convertRowIndexToModel(selec));
                    detalle.setIdsuministro(detalletempora.getCodigo());
                    detalle.setNomsuministro(detalletempora.getSuministro());
                    detalle.setPrecio(detalletempora.getPrecio());
                    detalle.setCantidad(detalletempora.getCantidad());
                    detalle.setValor(detalle.getPrecio().multiply(detalle.getCantidad()).setScale(5));
                    this.jpPadre.agregaDetalleSuministro(detalle);
                    this.jpPadre.codigoTemporal=detalletempora.getCodTemporal();
                    this.jpPadre.jtfSerie.setText(detalletempora.getSerie());
                    this.jpPadre.jtfNumero.setText(detalletempora.getNumero());
                    this.V_padre.dispose();
                    
            }
            else{
                   Utiles_MensajesDialogo.mensajeAlerta("No se ha seleccionado un suministro");
            }
           
        } catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("No se pudo guardar el detalle\n" + e.getMessage());
        }
    }//GEN-LAST:event_jbtAceptarActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        this.V_padre.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbCancelar.doClick();
        }
    }//GEN-LAST:event_jbCancelarKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbtAceptar;
    // End of variables declaration//GEN-END:variables

}
